
<template>
  <span class="ecp-dialog-btn">
    <el-button :disabled="disabled" type="text" @click="initForm">修改分类</el-button>
    <el-dialog
      title="修改分类"
      :visible.sync="visabled"
      width="320px"
      top="212px"
      center>
      <el-form :model="dialogForm" ref="dialogForm" label-width="90px" class="demo-ruleForm">
        <el-form-item
          label="分类名称"
          prop="name"
          :rules="[
            { required: true, message: '请输入分类名称'}
          ]"
        >
          <el-input type="text" v-model="dialogForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="排序"
          prop="orderIndex"
          :rules="[
            { required: true, message: '请输入排序'},
            { type: 'number', message: '排序必须为数字值'}
          ]"
        >
          <el-input type="text" v-model.number="dialogForm.orderIndex" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </span>
</template>

<script>
import { updateShopCategory } from "~/api/";
  export default {
    props: {
      disabled: {
        type: Boolean,
        default: false
      },
      formId: {
        require: true
      },
      formName: {
        require: true
      },
      formOrderIndex: {
        require: true
      },
    },
    data() {
      return {
        visabled: false,
        dialogForm: {
          name: '',
          orderIndex: 1
        }
      }
    },
    methods: {
      initForm () {
        this.dialogForm.name = this.formName
        this.dialogForm.orderIndex = this.formOrderIndex
        this.visabled = true
      },
      submitForm () {
        this.$refs['dialogForm'].validate( async (valid) => {
          if (!valid) return
          const res = await updateShopCategory(this, this.formId, this.dialogForm.name, this.dialogForm.orderIndex)
          // 成功处理
          if (res.code == '0') {
            this.$message.success('保存成功')
            this.$emit('on-success', this.dialogForm)
            this.visabled = false
            return
          }
          // 失败处理
          this.$msgbox({
            title: '提示',
            message: res.message,
            type: 'error'
          })
        })
      },
      resetForm() {
        this.$refs['dialogForm'].resetFields()
        this.visabled = false
      }
    }
  }
</script>

<style lang="scss" scoped>
.ecp-dialog-btn {
  & /deep/ {
    .el-dialog--center .el-dialog__body {
      padding: 15px;
    }
  }
}
</style>
